//
//  YHHoemAgentDetailsViewController.m
//  YHApp
//
//  Created by -- on 2021/7/25.
//

#import "HAHoemAgentDetailsVC.h"
#import "SGPagingView.h"
#import "HAHoemAgentDetailsSubVC.h"
#import "HAHoemAgentDetailsInfoVC.h"

@interface HAHoemAgentDetailsVC ()<SGPageTitleViewDelegate,SGPageContentViewDelegate>

@property (nonatomic, strong) SGPageTitleView *pageTitleView;

@property (nonatomic, strong) SGPageContentView *pageContentView;
/***/
@property (nonatomic, strong) UILabel *numLabel;
/***/
@property (nonatomic, strong) UILabel *nameLabel;
/***/
@property (nonatomic, assign) NSInteger selectedIndex;
/***/
@property (nonatomic, strong) NSMutableDictionary *numDict;

@end

@implementation HAHoemAgentDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"代理详情";
    self.numDict = [NSMutableDictionary dictionary];
    self.selectedIndex = 0;
    
    [self setupUI];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hl_tradeTotal:) name:@"kHoemAgentDetailstradeTotalNotificationID" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hl_name:) name:@"kHoemAgentDetailstradeNameNotificationID" object:nil];

    // Do any additional setup after loading the view.
}

-(void)hl_tradeTotal:(NSNotification*)noti {
    NSString *selectedIndexText = [NSString stringWithFormat:@"%ld",(long)self.selectedIndex];
    [self.numDict setValue:noti.object ?:@"" forKey:selectedIndexText];

    if ([[self.numDict allKeys] containsObject:selectedIndexText]) {
        self.numLabel.text = self.numDict[selectedIndexText];
    }
}
-(void)hl_name:(NSNotification*)noti {
    self.nameLabel.text = noti.object;
}

-(void)mButtonClick {
    HAHoemAgentDetailsInfoVC *vc = [[HAHoemAgentDetailsInfoVC alloc]init];
    vc.agent_id = self.agent_id;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setupUI {
        
    UIView *topBgV = [UIView ha_createView];
    topBgV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topBgV];
    [topBgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(app_topH+44);
        make.height.mas_offset(152);
    }];
    
    UIView *topView = [UIView ha_createView];
    topView.backgroundColor = [UIColor colorWithHexString:@"#373F60"];
    topView.layer.cornerRadius = 4;
    topView.layer.masksToBounds = YES;
    [topBgV addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(16);
        make.height.mas_offset(120);
    }];
    
    FL_Button *mButton = [[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusCenter];
    [mButton setTitle:@"详情" forState:UIControlStateNormal];
    [mButton setTitleColor:[UIColor colorWithHexString:@"#858b9c"] forState:UIControlStateNormal];
    mButton.titleLabel.font = [UIFont systemFontOfSize:12];
    mButton.fl_padding = 8;
    [mButton setImage:[UIImage imageNamed:@"ic_cell_more"] forState:UIControlStateNormal];
    [mButton addTarget:self action:@selector(mButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:mButton];
    [mButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(16);
        make.width.mas_offset(56);
        make.height.mas_offset(20);
    }];
    
    UILabel *nameLabel = [UILabel ha_BLabelWithFont:16 textColor:@"#ffffff" text:self.name alignment:0];
    self.nameLabel = nameLabel;
    [topView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(mButton);
        make.right.mas_equalTo(mButton.mas_left);
    }];

    UILabel *titLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:@"交易总金额" alignment:0];
    [topView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(nameLabel.mas_bottom).offset(15);
        make.right.mas_equalTo(-20);
    }];

    UILabel *numLabel = [UILabel ha_NLabelWithFont:26 textColor:@"#ffffff" text:@"0.00" alignment:0];
    self.numLabel = numLabel;
    [topView addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(titLabel.mas_bottom).offset(15);
        make.right.mas_equalTo(-20);
    }];

    NSMutableArray *titArr = [NSMutableArray array];
    NSMutableArray *vcArr = [NSMutableArray array];

    [titArr addObject:@"今年业绩"];
    [titArr addObject:@"当月业绩"];
    [titArr addObject:@"当日业绩"];

    HAHoemAgentDetailsSubVC *vc0 = [[HAHoemAgentDetailsSubVC alloc]init];
    vc0.type = 1;
    vc0.agent_id = self.agent_id;
    HAHoemAgentDetailsSubVC *vc1 = [[HAHoemAgentDetailsSubVC alloc]init];
    vc1.type = 2;
    vc1.agent_id = self.agent_id;
    HAHoemAgentDetailsSubVC *vc2 = [[HAHoemAgentDetailsSubVC alloc]init];
    vc2.type = 3;
    vc2.agent_id = self.agent_id;
    [vcArr addObject:vc0];
    [vcArr addObject:vc1];
    [vcArr addObject:vc2];

    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, app_topH+44+152, kUIScreen_w, 44) delegate:self titleNames:titArr titleFont:[UIFont systemFontOfSize:18]];
    [self.view addSubview:self.pageTitleView];
    self.pageTitleView.isTitleGradientEffect = NO;
    self.pageTitleView.indicatorLengthStyle = SGIndicatorLengthStyleEqual;
    self.pageTitleView.isNeedBounces = NO;
    self.pageTitleView.titleColorStateNormal = [UIColor colorWithHexString:@"#666F83"];
    self.pageTitleView.titleColorStateSelected = [UIColor colorWithHexString:@"#21252A"];
    self.pageTitleView.indicatorColor = [UIColor colorWithHexString:@"#000000"];
    self.pageTitleView.indicatorHeight = 1;
    self.pageTitleView.titleTextScaling = 0;
    self.pageTitleView.isOpenTitleTextZoom = YES;
    self.pageTitleView.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6F"];

    CGFloat contentViewHeight = kUIScreen_h-app_topH-64-152-44-5;
    self.pageContentView = [[SGPageContentView alloc] initWithFrame:CGRectMake(0, app_topH+44+152+44+5, kUIScreen_w, contentViewHeight) parentVC:self childVCs:vcArr];
    self.pageContentView.isScrollEnabled = YES;
    self.pageContentView.delegatePageContentView = self;
    [self.view addSubview:_pageContentView];

    [self.pageContentView setPageCententViewCurrentIndex:self.selectedIndex];
    self.pageTitleView.selectedIndex = self.selectedIndex;

}

- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    self.selectedIndex = selectedIndex;
    NSString *selectedIndexText = [NSString stringWithFormat:@"%ld",(long)self.selectedIndex];
    if ([[self.numDict allKeys] containsObject:selectedIndexText]) {
        self.numLabel.text = self.numDict[selectedIndexText];
    }
    
    [self.pageContentView setPageCententViewCurrentIndex:selectedIndex];
}

- (void)pageContentView:(SGPageContentView *)pageContentView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
    self.selectedIndex = targetIndex;
    NSString *selectedIndexText = [NSString stringWithFormat:@"%ld",(long)self.selectedIndex];
    if ([[self.numDict allKeys] containsObject:selectedIndexText]) {
        self.numLabel.text = self.numDict[selectedIndexText];
    }

    self.pageTitleView.resetSelectedIndex = targetIndex;
}


@end
